#include <getopt.h>
#include <stdio.h>
#include "string-util.h"

int sym_find_main(int argc,char **argv);
int sym_add_main(int argc,char **argv);
int sym_modify_main(int argc,char **argv);

void sym_print_help()
{
    const char *help_doc =
"sym [SYM-OPTIONS] [ACTION] [ACTION-CMDLINE]\n"
"SYM-OPTIONS 可取值:\n"
"-h|--help;输出当前帮助文档\n"
"ACTION 可取值如下;若省略 ACTION 则默认为 find. \n"
"add;表明追加一个符号到数据库中\n"
"find;表明从数据库中查找符号\n"
"modify;修改指定符号的内容\n"
"ACTION-CMDLINE;根据 ACTION 的取值来进行解释;可以使用 sym ACTION -h 来查看 ACTION 的\n"
"  的帮助文档;如: sym add -h";
    puts(help_doc);
    return ;
}


int
sym_main(int argc,char **argv)
{
    int ret = 0;
    if (argc < 2) {
        sym_print_help();
        return 1;
    }
    const char *action = argv[1];
    if (strequal(action,"find")) {
        --argc;
        ++argv;
        ret = sym_find_main(argc,argv);
    } else if (strequal(action,"modify")) {
        --argc;
        ++argv;
        ret = sym_modify_main(argc,argv);
    } else if (strequal(action,"add")) {
        --argc;
        ++argv;
        ret = sym_add_main(argc,argv);
    } else { // 表明用户未指定 ACTION,此时取默认 ACTION.
        struct option options[] = {
            {"help",no_argument,0,'h'},
            {NULL,0,0,0}
        };
        int getopt_ret;
        opterr = 0;
        optind = 0;
        while ((getopt_ret = getopt_long(argc,argv,"-h",options,0)) != -1) {
            switch (getopt_ret) {
            case 'h':
                sym_print_help();
                return 0;
            default:
                break;
            }
        }
        ret = sym_find_main(argc,argv);
    }
    return ret;
}
